/*******************************************************************************
 * Copyright (c) 2006 Ultimate Technology, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bojan Vukojevic - initial API and implementation
 *******************************************************************************/
package com.ultimatetech.cim;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.ultimatetech.cim.dialogs.UsernamePasswordDialog;

/**
 * The main plugin class to be used in the desktop.
 */
public class CIMPlugin extends AbstractUIPlugin {
//	The shared instance.
	private static CIMPlugin plugin;
	private Map imgMap;
	private String cimUser = null;
	private String cimPassword = null;
	/**
	 * The constructor.
	 */
	public CIMPlugin() {
		plugin = this;
		imgMap = new HashMap();
		loadImages();
	}
	
	private void loadImages(){
		try {
		imgMap.put("icons/class_obj.gif", 
				getImageDescriptor("icons/class_obj.gif").createImage());
		imgMap.put("icons/methpub_obj.gif", 
				getImageDescriptor("icons/methpub_obj.gif").createImage());
		imgMap.put("icons/assoc_class_obj.gif", 
				getImageDescriptor("icons/assoc_class_obj.gif").createImage());
		imgMap.put("icons/walk_assoc_obj2.gif", 
				getImageDescriptor("icons/walk_assoc_obj2.gif").createImage());
		imgMap.put("icons/field_private_obj.gif", 
				getImageDescriptor("icons/field_private_obj.gif").createImage());
		imgMap.put("icons/instance_obj.gif", 
				getImageDescriptor("icons/instance_obj.gif").createImage());
		imgMap.put("icons/search.gif", 
				getImageDescriptor("icons/search.gif").createImage());
		imgMap.put("icons/login.gif", 
				getImageDescriptor("icons/login.gif").createImage());
		imgMap.put("icons/key.gif", 
				getImageDescriptor("icons/key.gif").createImage());
		imgMap.put("icons/save.gif", 
				getImageDescriptor("icons/save.gif").createImage());
		imgMap.put("icons/save_disabled.gif", 
				getImageDescriptor("icons/save_disabled.gif").createImage());
		} catch (Throwable t){
			t.printStackTrace();
		}
	}

	public Image getImage(String name){
		Image i = (Image)imgMap.get(name);
		if(i == null){
			imgMap.put(name, 
					getImageDescriptor(name).createImage());
			i = (Image)imgMap.get(name);
		}
		return i;
	}
	
	public synchronized String getCIMUsername(){
		if(cimUser == null){
			//open dialog
			UsernamePasswordDialog upDiag = new UsernamePasswordDialog(
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
			upDiag.open();//does it block here?
			if(upDiag.getReturnCode() == Dialog.OK) {
			cimUser = upDiag.getEnteredUsername();
			cimPassword = upDiag.getEnteredPassword();
			}
			
		}
		return cimUser;
	}
	
	public synchronized String getCIMPassword() {
		if(cimPassword == null){
			//open dialog
			UsernamePasswordDialog upDiag = new UsernamePasswordDialog(
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
			upDiag.open();//does it block here?
			if(upDiag.getReturnCode() == Dialog.OK) {
			cimUser = upDiag.getEnteredUsername();
			cimPassword = upDiag.getEnteredPassword();
			}
		}
		return cimPassword;
	}
	
	public synchronized void setCIMUsername(String user){
		cimUser = user;
	}
	public synchronized void setCIMPassword(String pass){
		cimPassword = pass;
	}
	
	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 */
	public static CIMPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("com.ultimatetech.cim", path);
	}
	
}
